package org.lecric.wind44.client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.lecric.wind44.client.model.ISpot;
import org.lecric.wind44.client.model.Spots;
import org.lecric.wind44.client.service.SpotService;
import org.lecric.wind44.client.service.SpotServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class SpotUtils {
	public static final SpotServiceAsync SPOT_SERVICE = GWT.create(SpotService.class);

	private static Spots cachedSpots;

	public static void getSpots(final AsyncCallback<Spots> callback)
	{
		if(cachedSpots==null)
		{
			SPOT_SERVICE.getSpots(new AsyncCallback<Spots>(){
				@Override
				public void onFailure(Throwable caught) {
					callback.onFailure(caught);
				}
				@Override
				public void onSuccess(Spots result) {
					cachedSpots=result;
					callback.onSuccess(result);
				}
			});
		}
		else
			callback.onSuccess(cachedSpots);
	}
	
	public static <T extends ISpot> List<T> sort(List<T> spots)
	{
		ArrayList<T> sortedSpots=new ArrayList<T>();
		sortedSpots.addAll(spots);
		Collections.sort(sortedSpots);
		return sortedSpots;
	}
}
